package com.yuexunit.operationman.nbsy.module.AfterOperation.ui;

import android.content.Context;
import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.base.SubmitBean;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.AfterOperation.model.impl.AfterOperationModel;
import com.yuexunit.operationman.nbsy.module.AfterOperation.presenter.IAfterOperationPresenter;
import com.yuexunit.operationman.nbsy.module.AfterOperation.presenter.impl.AfterOperationPresenter;
import com.yuexunit.operationman.nbsy.module.AfterOperation.ui.view.IAfterOperationView;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2017/3/1.
 */
public class Act_AfterOperation_ObserveList extends BaseKJActivity implements IAfterOperationView {

    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;
    @Bind(R.id.afteroperationmain_observeform_other_edt)
    EditText afteroperationmainObserveformOtherEdt;

    LoadingDialog loadingDialog;
    IAfterOperationPresenter presenter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_afteroperation_observelist);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        loadingDialog = new LoadingDialog(this);
        presenter = new AfterOperationPresenter(this, new AfterOperationModel());
        setTitle();
        setContent();
    }

    private void setTitle() {
        tvPatientName.setText(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().getPatientName());
        if (AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().getPatientSex().equals("1")) {
            tvPatientSex.setText("男");
        } else if (AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().getPatientSex().equals("2")) {
            tvPatientSex.setText("女");
        } else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText("住院号" + AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().getInHospitalNo());
        tvPatientDisease.setText(PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_DISEASE));
    }

    private void setContent() {
        if(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().getAoOther() != null)
        {
            afteroperationmainObserveformOtherEdt.setText(AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().getAoOther());
        }
    }

    @OnClick({R.id.titlebar_docadvice_img_back, R.id.afteroperationmain_observelist_ll_01,
            R.id.afteroperationmain_observelist_ll_02,R.id.afteroperationmain_observeform_btn})
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.afteroperationmain_observelist_ll_01:
                intent = new Intent(aty, Act_AfterOperation_Observeform.class);
                intent.putExtra("type", 1);
                startActivity(intent);
                break;
            case R.id.afteroperationmain_observelist_ll_02:
                intent = new Intent(aty, Act_AfterOperation_Observeform.class);
                intent.putExtra("type", 2);
                startActivity(intent);
                break;
            case R.id.afteroperationmain_observeform_btn:
                insertContent();
                break;
        }
    }

    private void insertContent() {
        if(afteroperationmainObserveformOtherEdt.getText().length() > 0)
        {
            AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().setAoOther(afteroperationmainObserveformOtherEdt.getText().toString());
        }

        if (AppConfig.AFTEROPERATIONBEAN.getObject().getAoVisit().getAoVisitId() == null) {
            Map<String, String> params = new HashMap<>();
            params.put("operationNo", PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_OPERATION_ID) + "");
            params.put("saveData", JSON.toJSONString(AppConfig.AFTEROPERATIONBEAN));
            presenter.insertAfterOperationDate(params);
        } else {
            Map<String, String> params = new HashMap<>();
            params.put("accountId", PreferenceHelper.readInt(aty, AppConfig.USER_INFO, AppConfig.USER_ACCOUNT) + "");
            params.put("fullName", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_FULLNAME) + "");
            params.put("saveData", JSON.toJSONString(AppConfig.AFTEROPERATIONBEAN));
            presenter.updateAfterOperationDate(params);
        }
    }


    @Override
    public void insertAfterOperationDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void updateAfterOperationDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }

    /**
     * 通过dispatchTouchEvent每次ACTION_DOWN事件中动态判断非EditText本身区域的点击事件，然后在事件中进行屏蔽。然后当点击不是EditText时候，进行隐藏键盘操作
     *
     * @param ev
     * @return
     */

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {

        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件\
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
}
